/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-07-21
 * Time: 22:25
 */
public class Test {
    /**
     * 直接选择排序的优化
     * @param arr
     */

    public void selectSort2(int[] arr) {
        int left = 0;
        int right = arr.length-1;
        while (left < right) {
            int maxIndex = left;
            int minIndex = left;
            for (int i = left+1; i <= right ; i++) {
                if(arr[i] < arr[minIndex]) {
                    minIndex = i;
                }
                if (arr[i] > arr[maxIndex]) {
                    maxIndex = i;
                }
            }
            swap(arr,left,minIndex);
            //当最大值正好是  left下标时  此时 把最大值换到了minIndex的位置了
            if (maxIndex == left) {
                maxIndex = minIndex;
            }
            swap(arr,right,maxIndex);
            left++;
            right--;
        }
    }
}
